package com.fyzk.activity.forum;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tyjc.lottery.R;
import com.fyzk.utils.GlideUtils;
import com.fyzk.utils.ScreenUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import http.DataUtil.text.TextUtil;

/**
 * Created by fanxs11 on 17/5/9.
 */

public class TopicListAdapter extends BaseAdapter {

    private Context context;
    private List<TopicListBean> infos = new ArrayList<>();

    public TopicListAdapter(Context context, List<TopicListBean> infos) {
        this.context = context;
        this.infos = infos;
    }

    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public Object getItem(int position) {
        return infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TopicListBean info = infos.get(position);
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_topiclist, null);
            holder.icon = (ImageView) convertView.findViewById(R.id.topiclist_item_icon);
            holder.userName_tv = (TextView) convertView.findViewById(R.id.topiclist_item_username);
            holder.time_tv = (TextView) convertView.findViewById(R.id.topiclist_item_time);
            holder.title_tv = (TextView) convertView.findViewById(R.id.topiclist_item_title);
            holder.content_tv = (TextView) convertView.findViewById(R.id.topiclist_item_content);
            holder.imgs_ll = (LinearLayout) convertView.findViewById(R.id.topiclist_item_imgs);
            holder.reply_tv = (TextView) convertView.findViewById(R.id.topiclist_item_replycount);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Glide.with(context).load(info.getUser_info().getAvatar_file()).error(R.drawable.forum_header).transform(new GlideUtils(context)).into(holder.icon);
        if (TextUtil.isEmpty(info.getUser_info().getAlias())) {
            holder.userName_tv.setText(info.getUser_info().getUser_name());
        } else {
            holder.userName_tv.setText(info.getUser_info().getAlias());
        }
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
        holder.time_tv.setText(format.format(info.getAdd_time()*1000));
        holder.title_tv.setText(info.getQuestion_content());
        String text = info.getQuestion_detail();

        holder.imgs_ll.removeAllViews();
        if (text != null && text.contains("[/attach]")) {       // 内容中带图片

            if (info.getImageBeens() != null) {
                for (int i = 0; i < 3; i++) {
                    ImageView imageView = new ImageView(context);
                    int margin = 10;
                    int width = ScreenUtils.getInstance().getWidth() / 3 - margin*2;
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, width);
                    layoutParams.setMargins(margin,margin,margin,margin);
                    layoutParams.weight = 1;
                    imageView.setLayoutParams(layoutParams);
                    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    if (i < info.getImageBeens().size()) {
                        Glide.with(context).load(info.getImageBeens().get(i).thumb).into(imageView);
                    }
                    holder.imgs_ll.addView(imageView);
                }
            }

        }

        String html = getHtmlText(info);
        if (html != null) {
            holder.content_tv.setText(Html.fromHtml(html, new MImageGetter(holder.content_tv, context), null));
        }

        holder.reply_tv.setText(info.getAnswer_count()+"回帖");

        return convertView;
    }

    private String getHtmlText(TopicListBean headInfo) {
        String text = headInfo.getQuestion_detail();
        String htmlText = "";
        if (text != null && text.contains("[attach]")) {
            String[] contents = text.split("\\[/attach]");
            for (int i = 0; i < contents.length; i++) {
                String[] t_i = contents[i].split("\\[attach]");
                if (t_i[0] != null && !t_i[0].equals("")) {
                    htmlText += t_i[0];
                }
            }
        } else {
            htmlText = text;
        }
        if (htmlText != null && htmlText.contains("\n")) {
            htmlText = htmlText.replaceAll("\n", " ");
        }
        return htmlText;
    }

    public void setInfos(List<TopicListBean> infos) {
        this.infos = infos;
        this.notifyDataSetChanged();
    }

    class ViewHolder{
        ImageView icon;
        TextView userName_tv;
        TextView time_tv;
        TextView title_tv;
        TextView content_tv;
        LinearLayout imgs_ll;
        TextView reply_tv;
    }
}
